package cz.anime.ppro.beans;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.logic.postinicialize.PostInitialize;
import cz.anime.ppro.model.Webuser;

@Component
//@ManagedBean
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class LoginBean implements Serializable {

	private static final long serialVersionUID = -2620577383362138943L;
	
	@Autowired
	private UserComponent userComponent;

	private List<Webuser> users;

	private String login;

	private String password;

	@PostInitialize
	public void init() {
		users = userComponent.getUsers();

	}

	public boolean isLogged() {
		return !getLoggedUsername().equals("Anonymous");
	}
	
	public boolean isLoggedAsAdmin(){
		return userComponent.hasCurrentUserAdminRole();
	}

	public String getLoggedUsername() {
		return userComponent.getCurrentUsernameFromSession();
	}

	public List<Webuser> getUsers() {
		return users;
	}

	public void setUsers(List<Webuser> users) {
		this.users = users;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
